<?php
namespace app\index\model;

use think\Model;
use traits\model\SoftDelete;
use think\Db;


class ChangeModel extends Model
{

    protected $table = 'staff';

    public function change()
    {
        $link = Db::connect();
        $list = $link->table('table5')->select();
        foreach ($list as $project) {
            $a = json_decode($project['image']);
            $position = strpos($project['_id'], '(') + 1;
            $project['_id'] = substr($project['_id'], $position, -1);
            foreach ($a as $var) {
                $data = [
                    'content' => $var,
                    'projectId' => $project['_id']
                ];
                Db::table('image')->insert($data);
            }
        }
        $image = $link->table('image')->select();
        dump($image);
    }

    /**
     * 将分类拆分
     */
    public function classchildren()
    {
        $link = Db::connect();
        // 获取从 MongoDB 导入到 MySql 的原始数据
        $list = $link->table('projectclasses')->select();
        foreach($list as $projectclass) {
            $fData = [
                'name' => $projectclass['name'],
                'icon' => $projectclass['icon'],
                'order_id' => $projectclass['orderID']
            ];
            // 将数据拆分父分类存储在新数据表并返回父分类的 ID
            $fatherID = Db::table('projects_father_class')->insertGetId($fData);
            // 将未正确存储的子分类还原回 json 数据
            $a = json_decode($projectclass['children']);
            foreach ($a as $var) {
                $data = [
                    'cid' => $var->_id->{'$oid'},
                    'name' => $var->name,
                    'fatherID' => $fatherID
                ];
                // 拆分子分类存储在新数据表
                Db::table('projects_children_class')->insert($data);
            }
        }
    }

    /**
     * 分离方案
     */
    public function projects()
    {
        $link = Db::connect();
        // 获取从 MongoDB 导入到 MySql 的原始数据
        $list = $link->table('p')->select();
        foreach($list as $project) {
            // 获取方案对应的子分类信息，包括子分类 ID 和父分类 ID
            $childrenClass = $link->table('children_class')->where('cid', $project['projectClassId'])->find();
            $pData = [
                'project_id' => $project['project_id'],
                'title' => $project['titleName'],
                'children_class' => $childrenClass['id'],
                'father_class' => $childrenClass['father_id'],
                'orcode' => $project['ORCode'],
                'intro' => $project['intro'],
                'url' => $project['url'],
                'company' => $project['company'],
                'part_number' => $project['partNumber'],
                'price' => $project['price'],
                'recommend' => $project['recommend'],
                'collect' => $project['collect'],
                'likes' => $project['like'],
                'create_time' => $project['created_at'],
                'update_time' => $project['updated_at'],
                'old' => $project['_id']
            ];
            // 存储方案数据并返回方案 ID
            $projectID = Db::table('projects')->insertGetId($pData);
            // 将未正确存储的方案特点还原为 json 数据
            $features = json_decode($project['feature']);
            foreach($features as $var) {
                $Data = [
                    'features' => str_replace(PHP_EOL, '', $var),
                    'project_id' => $project['project_id']
                ];
                // 存储拆分后的方案特点到方案特点数据表
                Db::table('features')->insert($Data);
            }
            // 将未正确存储的方案功能还原为 json 数据
            $functions = json_decode($project['function']);
            foreach($functions as $var) {
                $Data = [
                    'functions' => str_replace(PHP_EOL, '', $var),
                    'project_id' => $project['project_id']
                ];
                // 存储拆分后的方案功能到方案功能数据表
                Db::table('functions')->insert($Data);
            }
            /**
             * 将未正确存储的方案图片还原为 json 数据
             * 
             * 0 是缩略图
             * 1 是方案框图
             * 2 是方案图片
             */
            // 缩略图
            $icon = [
                'path' => $project['icon'],
                'project_id' => $project['project_id'],
                'class' => '0'
            ];
            Db::table('images')->insert($icon);
            // 方案框图
            $diagram = [
                'path' => $project['diagram'],
                'project_id' => $project['project_id'],
                'class' => '1'
            ];
            Db::table('images')->insert($diagram);
            // 方案图片
            $images = json_decode($project['image']);
            foreach($images as $var) {
                $Data = [
                    'path' => $var,
                    'project_id' => $project['project_id'],
                    'class' => '2'
                ];
                // 存储拆分后的方案图片到方案图片数据表
                Db::table('images')->insert($Data);
            }
        }
    }

    /**
     * 分离用户
     */
    public function users()
    {
        $link = Db::connect();
        // 获取从 MongoDB 导入到 MySql 的原始数据
        $list = $link->table('aaa')->select();
        foreach($list as $user) {
            $data = [
                'username' => $user['username'],
                'password' => $user['password'],
                'is_admin' => $user['isAdmin'],
                'nick_name' => $user['nickname'],
                'email' => $user['email'],
                'gendar' => $user['gendar'],
                'age' => $user['age'],
                'grade' => $user['grade'],
                'create_time' => $user['created_at'],
                'update_time' => $user['updated_at'],
                'token' => $user['token'],
                'qq' => $user['qq'],
                'company' => $user['company'],
                'code' => $user['code'],
                'code_date' => $user['codeDate'],
                'phone' => $user['phone'],
                'pre' => $user['phonePre'],
                'is_active_email' => $user['isActiveEmail'],
                'is_active' => $user['isActive']
            ];
            // 存储用户数据并返回用户 ID
            $userID = Db::table('users')->insertGetId($data);
            // 将未正确存储的点赞列表还原为 json 数据
            $likes = json_decode($user['like']);
            foreach ($likes as $like) {
                // 获取对应的方案信息，包括方案 ID
                $project = $link->table('projects')->where('old', 'like', '%' . $like->{'$oid'} . '%')->find();
                $lData = [
                    'user_id' => $userID,
                    'project_id' => $project['project_id']
                ];
                // 存储拆分后的点赞列表到点赞数据表
                Db::table('likes')->insert($lData);
            }
            // 将未正确存储的收藏列表还原为 json 数据
            $collections = json_decode($user['collect']);
            foreach ($collections as $collection) {
                // 获取对应的方案信息，包括方案 ID
                $project = $link->table('projects')->where('old', 'like', '%' . $collection->{'$oid'} . '%')->find();
                $cData = [
                    'user_id' => $userID,
                    'project_id' => $project['project_id']
                ];
                // 存储拆分后的收藏列表到收藏数据表
                Db::table('collections')->insert($cData);
            }
        }
    }   

    /**
     * 更改 id
     */
    public function changid ()
    {
        $link = Db::connect();
        $flist = $link->table('projects_father_class')->select();
        // $clist = $link->table('projects_children_class')->select();
        foreach($flist as $father) {
            $clist = $link->table('projects_children_class')->where('fatherID', $father['fid'])->update(['fatherID' => $father['id']]);
        }
    }

    public function myTest()
    {
        $this->setTable('staff');
        return $this -> getData();
    }
}